/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pantallaConsultar.java
 *
 * Created on 06-ago-2010, 15:44:11
 */

package vista.comer;
import vista.comer.*;
import vista.interfaces.ICallBack;
import controlador.comer.GestorConsultarContactosResponsables;
import util.NTupla;
import util.Tupla;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import util.SwingPanel;
import vista.interfaces.IAyuda;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.Color;
/**
 *
 * @author Administrador
 */
public class pantallaConsultarContactosResponsables extends javax.swing.JInternalFrame implements ICallBack,   IAyuda {

    private GestorConsultarContactosResponsables gestor;
    private List<NTupla> listaContactos;
    private DefaultTableModel model;
    private boolean filtroBuscarActivado;

    /** Creates new form pantallaConsultar */
    public pantallaConsultarContactosResponsables()
    {
        initComponents();
        gestor = new GestorConsultarContactosResponsables(this);
        this.habilitarVentana();
        //listaNroTel= new ArrayList<String>();
        //listaTipoTel= new ArrayList<Tupla>();

    }

    private void habilitarVentana()
    {
        filtroBuscarActivado=false;
        cargarContactos();
        activarFiltrosTabla();
        if(tablaContactos.getSelectedRow()!=-1)
       {
           btnModificarContacto.setEnabled(true);
           btnConsultarContacto.setEnabled(true);
           //btnBajaContacto.setEnabled(true);

        }

    }

    private void cargarContactos()
    {

        listaContactos=gestor.listaContactos();
        /*for (int i= 0; i < listaEmpleados.size(); i++)
        {

           tablaEmpleados.getModel().. listaEmpleados.get(0)
        }
        */
        model = (DefaultTableModel) tablaContactos.getModel();
        int fil=model.getRowCount();
        for (int i = 0; i < fil; i++) {
            model.removeRow(0);
        }
        for (NTupla nTuplaEmpleado : listaContactos)
        {   
            //Creo un nuevo array con una unidad mas d largo que el devuelto en el Data de la NTupla(Para agregar el id)
            Object[] obj=new Object[((Object[])nTuplaEmpleado.getData()).length+1];
            Tupla tup=new Tupla();
            tup.setId(nTuplaEmpleado.getId());
            tup.setNombre(nTuplaEmpleado.getNombre());
            obj[0]=tup;

            //Este metodo d aca abajo copia el contenido del array de Data al nuevo array obj, poniendo los datos a partir d la posicion 1
            System.arraycopy((Object[]) nTuplaEmpleado.getData(), 0, obj, 1, ((Object[]) nTuplaEmpleado.getData()).length);
            model.addRow( obj );
        }
        //modeloTablaCompleto=model;
        tablaContactos.setModel(model);
        //TODO: Esconder primera fila  //tablaContactos.getColumnModel().removeColumn(tablaContactos.getColumnModel().getColumn(0));
        
    }

   public void activarFiltrosTabla()
    {
         TableRowSorter<TableModel> modeloOrdenado;
           // model.setRowFilter(RowFilter.regexFilter("2", 1));
            modeloOrdenado = new TableRowSorter<TableModel>(model);
            tablaContactos.setRowSorter(modeloOrdenado);


        if(filtroBuscarActivado)
        {
           String[] cadena=txtBuscar.getText().split(" ");
           List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>();
           for (int i= 0; i < cadena.length; i++)
           {
             filters.add(RowFilter.regexFilter("(?i)" + cadena[i]));
           }

           RowFilter<Object,Object> cadenaFilter = RowFilter.andFilter(filters);
           modeloOrdenado.setRowFilter(cadenaFilter);
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaContactos = new javax.swing.JTable();
        btnModificarContacto = new javax.swing.JButton();
        btnNuevoContacto = new javax.swing.JButton();
        btnConsultarContacto = new javax.swing.JButton();
        txtBuscar = new javax.swing.JTextField();

        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Consultar Contactos Asignados");
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search.png"))); // NOI18N

        tablaContactos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "CUIL", "Nombre", "Apellido"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaContactos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tablaContactos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaContactosMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tablaContactosMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(tablaContactos);

        btnModificarContacto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/text_page.png"))); // NOI18N
        btnModificarContacto.setText("Modificar");
        btnModificarContacto.setEnabled(false);
        btnModificarContacto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarContactoActionPerformed(evt);
            }
        });

        btnNuevoContacto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/add_page.png"))); // NOI18N
        btnNuevoContacto.setText("Nuevo");
        btnNuevoContacto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoContactoActionPerformed(evt);
            }
        });

        btnConsultarContacto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/iconos/var/16x16/search_page.png"))); // NOI18N
        btnConsultarContacto.setText("Ver Detalles");
        btnConsultarContacto.setEnabled(false);
        btnConsultarContacto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarContactoActionPerformed(evt);
            }
        });

        txtBuscar.setFont(new java.awt.Font("Tahoma", 2, 11));
        txtBuscar.setForeground(new java.awt.Color(102, 102, 102));
        txtBuscar.setText("Buscar...");
        txtBuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtBuscarMouseClicked(evt);
            }
        });
        txtBuscar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtBuscarFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtBuscarFocusLost(evt);
            }
        });
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtBuscarKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnConsultarContacto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnNuevoContacto, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnModificarContacto, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 555, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 342, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnConsultarContacto)
                    .addComponent(btnModificarContacto)
                    .addComponent(btnNuevoContacto))
                .addContainerGap(39, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarContactoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarContactoActionPerformed
        if(tablaContactos.getSelectedRow()!=-1)
        {
        int id;
        //String sid;
        id=((Tupla)(tablaContactos.getModel().getValueAt(tablaContactos.getSelectedRow(), 0) )).getId();
        //id=Integer.parseInt(sid);
        pantallaRegistrarContactoResponsable pre = new pantallaRegistrarContactoResponsable(id, this);
        SwingPanel.getInstance().addWindow(pre);
        pre.setVisible(true);
            //pre.opcionRegistrarEmpleado();
        }
    }//GEN-LAST:event_btnModificarContactoActionPerformed

    private void btnNuevoContactoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoContactoActionPerformed
        pantallaRegistrarContactoResponsable pre = new pantallaRegistrarContactoResponsable(this);
            SwingPanel.getInstance().addWindow(pre);
            pre.setVisible(true);
            //pre.opcionRegistrarEmpleado();
    }//GEN-LAST:event_btnNuevoContactoActionPerformed

    private void btnConsultarContactoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarContactoActionPerformed
        if(tablaContactos.getSelectedRow()!=-1)
        {
        Tupla tId;
            tId=(Tupla)(tablaContactos.getModel().getValueAt(tablaContactos.getSelectedRow(), 0) );
        pantallaConsultarDatosContactoResponsable pre = new pantallaConsultarDatosContactoResponsable(tId.getId(), this);
        SwingPanel.getInstance().addWindow(pre);
        pre.setVisible(true);
            //pre.opcionRegistrarEmpleado();
        }
    }//GEN-LAST:event_btnConsultarContactoActionPerformed

    private void tablaContactosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaContactosMouseClicked
       
    }//GEN-LAST:event_tablaContactosMouseClicked

    private void txtBuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtBuscarMouseClicked

        if(txtBuscar.getText().equals("Buscar...")) {
            txtBuscar.setText("");
        }
    }//GEN-LAST:event_txtBuscarMouseClicked

    private void txtBuscarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBuscarFocusGained

        if(txtBuscar.getText().equals("Buscar...")) {
            txtBuscar.setText("");
            txtBuscar.setForeground(Color.BLACK);
            filtroBuscarActivado=true;
        }
}//GEN-LAST:event_txtBuscarFocusGained

    private void txtBuscarFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtBuscarFocusLost
        if(txtBuscar.getText().equals("")) {
            txtBuscar.setText("Buscar...");
            txtBuscar.setForeground(Color.GRAY);
            filtroBuscarActivado=false;
        } else {
            filtroBuscarActivado=true;}
    }//GEN-LAST:event_txtBuscarFocusLost

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        activarFiltrosTabla();
}//GEN-LAST:event_txtBuscarKeyReleased

    private void txtBuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyTyped

    }//GEN-LAST:event_txtBuscarKeyTyped

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        
    }//GEN-LAST:event_formMouseReleased

    private void tablaContactosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaContactosMouseReleased
        if(tablaContactos.getSelectedRow()!=-1)
       {
           btnModificarContacto.setEnabled(true);
           btnConsultarContacto.setEnabled(true);
           //btnBajaContacto.setEnabled(true);
           if (evt.getClickCount() == 2)
            {

            Tupla tId;
            tId=(Tupla)(tablaContactos.getModel().getValueAt(tablaContactos.getSelectedRow(), 0) );

            pantallaConsultarDatosContactoResponsable pre = new pantallaConsultarDatosContactoResponsable(tId.getId(), this);
            SwingPanel.getInstance().addWindow(pre);
            pre.setVisible(true);
                //pre.opcionRegistrarEmpleado();
            }
        }
        else
        {
           btnModificarContacto.setEnabled(false);
           btnConsultarContacto.setEnabled(false);
        }
    }//GEN-LAST:event_tablaContactosMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnConsultarContacto;
    private javax.swing.JButton btnModificarContacto;
    private javax.swing.JButton btnNuevoContacto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaContactos;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables
public void actualizar(int flag,boolean exito) {
        //Flag:
        // 1: Viene del CU ConsultarDetallesEmpleado
        // 2: Viene del CU RegistrarNuevoEmpleado
         // 3: Viene del CU modificarEmpleado
       habilitarVentana();
    }
public int getIdAyuda()
    {
        return 0;
    }

    public String getResumenAyuda() {
        return "Registre un nuevo capacitador o seleccione uno existente y consulte o modifique sus datos";
    }

    public String getTituloAyuda()
    {
        return "Opción: Consultar Capacitadores";

    }
}
